function Tank(options) {
    this.init(options);
}

Tank.prototype = new Sprite();

Tank.prototype.radius = 25;
Tank.prototype.speed = 10;
Tank.prototype.action = function() {};

Tank.prototype.topOfBarrel = function() {
    switch(this.towards) {
    case "left":
        return {
            x: this.centerX - 2 * this.radius,
            y: this.centerY
        }
    case "up":
        return {
            x: this.centerX,
            y: this.centerY - 2 * this.radius
        }
    case "right":
        return {
            x: this.centerX + 2 * this.radius,
            y: this.centerY
        }
    case "down":
        return {
            x: this.centerX,
            y: this.centerY + 2 * this.radius
        }
    default:
        throw "Unknown tank towards: " + towards;
    };
};

function min(a, b) {
    return a <= b ? a : b;
}

function max(a, b) {
    return a >= b ? a : b;
}

Tank.prototype.move = function() {
    this.baseMove();
    this.centerX = max(this.centerX, this.radius);
    this.centerX = min(this.centerX, canvas.width - this.radius);
    this.centerY = max(this.centerY, this.radius);
    this.centerY = min(this.centerY, canvas.height - this.radius);
};

Tank.prototype.shoot = function() {
    var point = this.topOfBarrel();
    var bullet = new Bullet({
        centerX: point.x,
        centerY: point.y,
        direction: this.towards
    });
    bullets.push(bullet);
};

Tank.prototype.draw = function() {
    var centerX = this.centerX,
        centerY = this.centerY,
        radius = this.radius;
    ctx.beginPath();
    ctx.strokeRect(centerX - radius, centerY - radius, radius * 2, radius * 2);
    ctx.moveTo(centerX, centerY);
    var topOfBarrel = this.topOfBarrel();
    ctx.lineTo(topOfBarrel.x, topOfBarrel.y);
    ctx.stroke();
    ctx.beginPath();
    ctx.arc(centerX, centerY, radius / 2, 0, 2*Math.PI);
    ctx.stroke();
};
